package com.rfid.afd.presenter;

import android.os.Handler;

import com.rfid.afd.activity.view.ISyncView;
import com.rfid.afd.model.SyncModel;
import com.rfid.config.Constant;
import com.rfid.db.bean.Operation;
import com.rfid.rls_psw.App;
import com.rfid.util.FileUtil;
import com.rfid.util.UserManager;
import com.rfid.util.Util;

import java.io.IOException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class SyncPresenter extends BasePresenter<ISyncView> {
    protected ExecutorService mExecutor;
    protected Handler mHandler;
    private SyncModel mMode;

    public SyncPresenter() {
        mHandler = new Handler();
        mExecutor = Executors.newSingleThreadExecutor();
        mMode = new SyncModel();
    }

    public void upload() {
        mExecutor.execute(new Runnable() {
            @Override
            public void run() {
                int count = 0;
                boolean success = true;
                try {
                    notifyGenerateOperateFile();
                    String locateFilePath = Constant.FILE_DIR + "dw."+ UserManager.getInstance().getLoginUser().getName() +".PD0001."+ Util.getTime("yyyMMddHHmmss") +".dat";
                    String checkFilePath = Constant.FILE_DIR + "pd."+ UserManager.getInstance().getLoginUser().getName() +".PD0001."+ Util.getTime("yyyMMddHHmmss") +".dat";
                    String deleteFilePath = Constant.FILE_DIR + "tc."+ UserManager.getInstance().getLoginUser().getName() +".PD0001."+ Util.getTime("yyyMMddHHmmss") +".dat";
                    String shangjiaFilePath = Constant.FILE_DIR + "sj."+ UserManager.getInstance().getLoginUser().getName() +".PD0001."+ Util.getTime("yyyMMddHHmmss") +".dat";
                    String gatherFilePath = Constant.FILE_DIR + "cj."+ UserManager.getInstance().getLoginUser().getName() +".PD0001."+ Util.getTime("yyyMMddHHmmss") +".dat";
                    count = mMode.generateLocateFile(locateFilePath);
                    if(count > 0) {
                        try {
                            notifyUploadFile(locateFilePath);
                            generateOperateFile(count);
                            mMode.upLoadFile(locateFilePath);
                            notifyUploadSuccess();
                            mMode.markUploaded(Operation.OPERATE_LOCATE);
                        } catch (Exception e) {
                            e.printStackTrace();
                            uploadFail();
                            success = false;
                        }
                    }
                    count = mMode.generateCheckFile(checkFilePath);
                    if(count > 0) {
                        try {
                            notifyUploadFile(checkFilePath);
                            generateOperateFile(count);
                            mMode.upLoadFile(checkFilePath);
                            notifyUploadSuccess();
                            mMode.markUploaded(Operation.OPERATE_CHECK);
                        } catch (Exception e) {
                            e.printStackTrace();
                            uploadFail();
                            success = false;
                        }
                    }
                    count = mMode.generateDeleteFile(deleteFilePath);
                    if(count > 0) {
                        try {
                            notifyUploadFile(deleteFilePath);
                            generateOperateFile(count);
                            mMode.upLoadFile(deleteFilePath);
                            notifyUploadSuccess();
                            mMode.markUploaded(Operation.OPERATE_DELETE);
                        } catch (Exception e) {
                            e.printStackTrace();
                            uploadFail();
                            success = false;
                        }
                    }
                    count = mMode.generateShangjiaFile(shangjiaFilePath);
                    if(count > 0) {
                        try {
                            notifyUploadFile(shangjiaFilePath);
                            generateOperateFile(count);
                            mMode.upLoadFile(shangjiaFilePath);
                            notifyUploadSuccess();
                            mMode.markUploaded(Operation.OPERATE_SHANGJIA);
                        } catch (Exception e) {
                            e.printStackTrace();
                            uploadFail();
                            success = false;
                        }
                    }
                    count = mMode.generateGatherFile(gatherFilePath);
                    if(count > 0) {
                        try {
                            notifyUploadFile(gatherFilePath);
                            generateOperateFile(count);
                            mMode.upLoadFile(gatherFilePath);
                            notifyUploadSuccess();
                            mMode.markUploaded(Operation.OPERATE_SHANGJIA);
                        } catch (Exception e) {
                            e.printStackTrace();
                            uploadFail();
                            success = false;
                        }
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    generateFileFail();
                    success = false;
                }

                notifyUploadOver();

//                try {
//                    startCreateShunjia();
//                    count = mMode.createShunjiaFile(Constant.FILE_DIR + "shunjia.txt");
//                    shunjiaCreated(count);
//                    if(count > 0) {
//                        try {
//                            notifyUploadFile();
//                            mMode.upLoadFile(Constant.FILE_DIR + "shunjia.txt");
//                            notifyUploadSuccess();
////                            mMode.markUploaded(Operation.);
//                        } catch (Exception e) {
//                            e.printStackTrace();
//                            uploadFail();
//                            success = false;
//                        }
//                    }
//                } catch (IOException e) {
//                    e.printStackTrace();
//                    shunjiaFile();
//                    success = false;
//                }
            }
        });
    }

    public void download() {
        mExecutor.execute(new Runnable() {
            @Override
            public void run() {
                notifyDownloadStart();
                try {
                    mMode.downloadDBFile("/sdcard/rfid.db.cache");
                    FileUtil.rm("/sdcard/rfid.db-wal");
                    FileUtil.rm("/sdcard/rfid.db-shm");
                    FileUtil.mv("/sdcard/rfid.db.cache","/sdcard/rfid.db");
                    App.getInstance().initDb();
                    notifyDownloadSuccess();
                } catch (IOException e) {
                    e.printStackTrace();
                    notifyDownloadFail();
                }
            }
        });
    }

    private void notifyUploadOver() {
        mHandler.post(new Runnable() {
            @Override
            public void run() {
                mIview.onUploadOver();
            }
        });
    }

    private void notifyDownloadFail() {
        mHandler.post(new Runnable() {
            @Override
            public void run() {
                mIview.onDownloadFail();
            }
        });
    }

    private void notifyDownloadSuccess() {
        mHandler.post(new Runnable() {
            @Override
            public void run() {
                if(mIview != null)
                    mIview.onDownloadSuccess();
            }
        });
    }

    private void notifyDownloadStart() {
        mHandler.post(new Runnable() {
            @Override
            public void run() {
                if(mIview != null)
                    mIview.onDownloadBegain();
            }
        });
    }

    private void shunjiaFile() {
        mHandler.post(new Runnable() {
            @Override
            public void run() {
                if(mIview != null)
                    mIview.onShunjiaFileFail();
            }
        });
    }

    private void shunjiaCreated(int size) {
        mHandler.post(new Runnable() {
            @Override
            public void run() {
                if(mIview != null)
                    mIview.onShunjiaFileCreated(size);
            }
        });
    }

    private void startCreateShunjia() {
        mHandler.post(new Runnable() {
            @Override
            public void run() {
                if(mIview != null)
                    mIview.onShunjiaFileBegain();
            }
        });
    }

    private void uploadFail() {
        mHandler.post(new Runnable() {
            @Override
            public void run() {
                if(mIview != null)
                    mIview.onUploadFileFail();
            }
        });
    }

    private void generateFileFail() {
        mHandler.post(new Runnable() {
            @Override
            public void run() {
                if(mIview != null)
                    mIview.onOperateFileMakeFail();
            }
        });
    }

    private void notifyUploadSuccess() {
        mHandler.post(new Runnable() {
            @Override
            public void run() {
                if(mIview != null)
                    mIview.onUploadFileSuccess();
            }
        });
    }

    private void notifyUploadFile(String name) {
        mHandler.post(new Runnable() {
            @Override
            public void run() {
                if(mIview != null)
                    mIview.onUploadFileStart(name);
            }
        });
    }

    private void generateOperateFile(int count) {
        mHandler.post(new Runnable() {
            @Override
            public void run() {
                if(mIview != null)
                    mIview.onOperateFileMaked(count);
            }
        });
    }

    private void notifyGenerateOperateFile() {
        mHandler.post(new Runnable() {
            @Override
            public void run() {
                if(mIview != null)
                    mIview.onOperateFileBegain();
            }
        });
    }


    public long getUnUploadCount() {
        return mMode.getUnUploadItemCount();
    }
}
